package com.cityguardian.cityguardianapp;

import com.cityguardian.model.Alerta;
import com.cityguardian.model.Usuario;
import com.cityguardian.threads.AsyncTaskPostServicio;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class PostServiceActivity extends ActionBarActivity {
	
	private Usuario usuario;
	private Alerta alerta;
	private TextView editTextCausas;
	private TextView editTextCauso;
	private TextView editTextEstructuras;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_service);
		// Show the Up button in the action bar.
		setupActionBar();
		
		Intent intent = getIntent();
		usuario = (Usuario) intent.getSerializableExtra("Usuario");
		alerta = (Alerta) intent.getSerializableExtra("Alerta");
		editTextCausas = (EditText) findViewById(R.id.editTextCausas);
		editTextCauso = (EditText) findViewById(R.id.editTextCauso);
		editTextEstructuras = (EditText) findViewById(R.id.editTextEstructuras); 
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		} else {
		    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.post, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				NavUtils.navigateUpFromSameTask(this);
				return true;
	        case R.id.action_button:
	            sendUserForFillActivity();
	            return true;
	        case R.id.number_button:
	        	showSearch();
	        	return true;
	        case R.id.action_search:
	        	showSearch();
	        	return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void sendUserForFillActivity() {
		Intent intent = new Intent(this, FillAlertActivity.class);	
		intent.putExtra("Usuario", this.usuario);
		intent.putExtra("Latitud", Double.parseDouble(alerta.getPosicionY()));
		intent.putExtra("Longitud", Double.parseDouble(alerta.getPosicionX()));
		startActivity(intent);	
	}
		
	private void showSearch(){
		Intent intent = new Intent(this, SearchActivity.class);
		intent.putExtra("Usuario", usuario);
		intent.putExtra("Alerta", alerta);		
		startActivity(intent);
	}
	
	public void onEnviar(View view){
		AsyncTaskPostServicio thread = new AsyncTaskPostServicio(this,usuario,alerta,editTextCausas.getText().toString(),
				editTextCauso.getText().toString(),editTextEstructuras.getText().toString());
		thread.execute();
		
		//Toast.makeText(getBaseContext(), "onEnviar a que si?", Toast.LENGTH_LONG).show();
		
	}
}
